<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力中小企业发展
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
// +----------------------------------------------------------------------
// | Wdadmin系统产品软件并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
// +----------------------------------------------------------------------
/**
 * Class Bank
 * Desc  银行卡控制器
 * Create on 2024/9/26 11:59
 * Create by wangyafang
 */

namespace app\api\controller\wddj\driver;


use app\api\model\wddj\User;
use app\common\controller\Api;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
use Exception;


class Bank extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    protected $model = null;
    protected $driverObj = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\api\model\wddj\driver\Bank();
        $wechat_id = User::getWechatId($this->auth->id);
        $driverObj = (new \app\api\model\wddj\driver\Driver())->where('wechat_id',$wechat_id)->find();
        if (!$driverObj) {
            $this->error('不是司机，没有权限,请先认证');
        }
        $this->driverObj = $driverObj;
    }

    /**
     * Desc 列表
     * Create on 2024/9/26 14:22
     * Create by wangyafang
     */
    public function index(){
        if(!$this->request->isGet()) {
            $this->error('请求类型错误');
        }
        
        $wechat_id = User::getWechatId($this->auth->id);
        $where['wechat_id'] = array('eq',$wechat_id);
        $data = $this->model
            ->where($where)
            ->field('id,name,bank_name,bank_card_number,default_state')
            ->order('default_state asc,id desc')
            ->select();
        foreach ($data as $v) {
            if(is_numeric($v['bank_card_number'])) {
                $v->bank_card_number =   substr_replace($v['bank_card_number'], '********', 4, 8);
            }
        }
        $this->success('请求成功', $data);
    }

    /**
     * Desc 添加
     * Create on 2024/9/26 12:01
     * Create by wangyafang
     */
    public function add()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $param = $this->request->post();
        $result = $this->validate($param,'app\api\validate\wddj\driver\Bank');
        if(true !== $result){
            // 验证失败 输出错误信息
            $this->error($result);
        }

        $wechat_id = User::getWechatId($this->auth->id);

        $bankObj = $this->model->where('wechat_id',$wechat_id)
            ->where('name',$param['name'])
            ->where('bank_name',$param['bank_name'])
            ->where('bank_card_number',$param['bank_card_number'])
            ->find();
        if ($bankObj) {
            $this->error('银行卡已添加');
        }

        $create_data = array(
            'wechat_id'=>$wechat_id,
            'driver_id'=>$this->driverObj->id,
        );
        $create_data = array_merge($create_data,$param);

        Db::startTrans();
        try {
            $this->model->data($create_data);
            $this->model->allowField(true)->save();
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success('添加成功');
    }

    /**
     * Desc 编辑
     * Create on 2024/9/26 13:50
     * Create by wangyafang
     */
    public function edit()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $param = $this->request->post();

        if (!isset($param['id'])) {
            $this->error('参数错误');
        }

        $result = $this->validate($param,'app\api\validate\wddj\driver\Bank');
        if(true !== $result){
            // 验证失败 输出错误信息
            $this->error($result);
        }

        $id = $param['id'];

        $wechat_id = User::getWechatId($this->auth->id);
        $checkBankObj = $this->model->where('wechat_id',$wechat_id)
            ->where('id','<>',$id)
            ->where('name',$param['name'])
            ->where('bank_name',$param['bank_name'])
            ->where('bank_card_number',$param['bank_card_number'])
            ->find();
        if ($checkBankObj) {
            $this->error('银行卡已存在');
        }

        $bankObj = $this->model->get($id);

        if ($bankObj['wechat_id'] != $wechat_id) {
            $this->error('不是本人，无法操作');
        }

        Db::startTrans();
        try {
            $bankObj->save($param);
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success('编辑成功');
    }

    /**
     * Desc 设置默认
     * Create on 2024/9/26 14:13
     * Create by wangyafang
     */
    public function set_default_bank()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $id = $this->request->post('id');

        $bankObj = $this->model->get($id);
        if (!$bankObj) {
            $this->error('银行卡信息不存在');
        }
        $wechat_id = User::getWechatId($this->auth->id);
        if ($bankObj['wechat_id'] != $wechat_id) {
            $this->error('不是本人，无法操作');
        }

        $defaultBankObj = $this->model->where('wechat_id',$wechat_id)
            ->where('id','<>',$id)
            ->where('default_state','1')
            ->find();
        if ($defaultBankObj) {
            $defaultBankObj->default_state = '2';
            $defaultBankObj->save();
        }


        Db::startTrans();
        try {
            $bankObj->save(['default_state'=>'1']);
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success('设置成功');

    }

    /**
     * Desc 详情
     * Create on 2024/9/26 14:38
     * Create by wangyafang
     */
    public function details()
    {
        if(!$this->request->isGet()) {
            $this->error('请求类型错误');
        }
        $id = $this->request->get('id');

        $bankObj = $this->model->get($id);
        $wechat_id = User::getWechatId($this->auth->id);
        if ($bankObj['wechat_id'] != $wechat_id) {
            $this->error('不是本人，无法操作');
        }

        $bankObj->hidden(['wechat_id','default_state']);

        $this->success('请求成功',$bankObj);
    }

    /**
     * Desc 删除
     * Create on 2024/9/26 14:54
     * Create by wangyafang
     */
    public function del()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $id = $this->request->post('id');
        $bankObj = $this->model->get($id);
        if (!$bankObj) {
            $this->error('银行卡不存在');
        }

        $wechat_id = User::getWechatId($this->auth->id);
        if ($bankObj['wechat_id'] != $wechat_id) {
            $this->error('不是本人，无法操作');
        }

        $result = false;
        Db::startTrans();
        try {
            $result = $bankObj->delete();
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        if ($result == false) {
            $this->error('删除失败');
        }

        $this->success('删除成功');
    }

    /**
     * Desc 默认银行卡信息
     * Create on 2024/9/30 10:21
     * Create by wangyafang
     */
    public function default_bank_info()
    {
        if(!$this->request->isGet()) {
            $this->error('请求类型错误');
        }
        $wechat_id = User::getWechatId($this->auth->id);
        $data = $this->model
            ->where('wechat_id',$wechat_id)
            ->field('id,name,bank_name,bank_card_number')
            ->order('default_state asc,id desc')
            ->find();
        if ($data) {
            if(is_numeric($data['bank_card_number'])) {
                $data['bank_card_number'] = substr_replace($data['bank_card_number'], '********', 4, 8);
            }
        }
        $this->success('请求成功',$data);
    }
}



 